##
# @package Resistor::Spec::ControllerHelper
# A collection of methods to assist in RSpec tests.  includes method to simulate ActiveRback current_user
#
# @author Chris Scott
#
module Resistor::Spec
    module ControllerHelper

        ##
        # authenticate_user
        # creates a current_user mock to simulate ActiveRbac's controller.current_user
        #
        def authenticate_user

            # mock a dummy Account model
            account = mock_model(Account, :id => 1)

            # mock ActiveRbac's User
            current_user = mock(User, :account => account, :root_domain => 'corp')

            # stub the AuthN methods to return true
            current_user.stub!(:has_role?).and_return(true)
            current_user.stub!(:authenticated?).and_return(true)

            # stub current_user method to controller
            controller.stub!(:current_user).and_return(current_user)
        end

        ##
        # verify_response
        # verifies that an Ajax json response was successful
        # TODO: figure out a way to test actual RResponse instance instead of just testing content_type is
        # "application/json".  this content_type is generated by RException as well.
        #
        def verify_response
            # this line will fail if rres contains an RException
            controller.rres.should be_a_kind_of(RResponse)
            response.content_type.should == "application/json"
        end

        ##
        # verify_exception
        # verifies that an RException occured
        #
        def verify_exception
            # this line will fail if rres contains an RException
            controller.exception.should be_a_kind_of(RException)
            response.content_type.should == "application/json"
        end
    end
end